home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 06 - 1990 / 06.09 Sep 90 / TEDemo / TEDemo.cp next >
Encoding:
Text File  |  1990-06-24  |  17.8 KB  |  715 lines  |  [TEXT/MPS ]

  1. #include <UMacApp.h>
  2. #include <UPrinting.h>
  3. #include <UTEView.h>
  4. #include <Fonts.h>
  5. #include <ToolUtils.h>
  6.  
  7. #include "TEDemo.h"
  8.  
  9. const OSType kSignature     = 'JLMT';
  10. const OSType kFileType         = 'JL01';
  11. const int    kWindID            =    1001;    // This is passed to NewSimpleWindow
  12. const int    kHelpID         =     1001;    // Help dialog uses DLOG 1001
  13. const int    kPaletteWidth    =    640;
  14. const int    kPaletteHeight    =    32;
  15. const int    kIconWidth        =    32;
  16. const int    kBoxIconID        =    256;
  17. const int    kTextIconID        =    257;
  18. const int    kBox            =    1;
  19. const int    kText            =    2;
  20. const int    kPaletteColor     =    greenColor;
  21. const int    kBoxColor        =    redColor;
  22.  
  23. // commands
  24. const int    cHelp             =    1001;    // for color menu items}
  25. const int    cDrawBox        =    2001;    // command number for Box sketcher object}
  26.  
  27. const int    cBlack             =    5001;    // for TextColor menu
  28. const int    cBlue             =    5002;
  29. const int    cGreen             =    5003;
  30. const int    cRed             =    5004;
  31. const int    cWhite             =    5005;
  32.        
  33. int        gColorArray[cWhite-cBlack+1];
  34. Rect    gIconRect[kText-kBox+1];
  35.  
  36. pascal void TTEApplication::ITEApplication(OSType itsMainFileType)
  37. {
  38.     gColorArray[cBlack-cBlack]    = blackColor; 
  39.     gColorArray[cBlue-cBlack]     = blueColor;
  40.     gColorArray[cGreen-cBlack]    = greenColor;
  41.     gColorArray[cRed-cBlack]     = redColor;
  42.     gColorArray[cWhite-cBlack]    = whiteColor;
  43.     SetRect(&gIconRect[kBox-kBox], 0, 0, kIconWidth, kIconWidth); // left  top  right  bottom
  44.     SetRect(&gIconRect[kText-kBox], kIconWidth, 0, 2 * kIconWidth, kIconWidth);
  45.     IApplication(itsMainFileType);
  46.     RegisterStdType("\pTTextView", 'text');
  47.     if (gDeadStripSuppression)
  48.     {
  49.         TTextView *aTextView;
  50.         aTextView = new TTextView;
  51.     }
  52.     InitPrinting();
  53. }
  54.  
  55. pascal struct TDocument *TTEApplication::DoMakeDocument(CmdNumber itsCmdNumber)
  56. {
  57.  
  58.     TTEDocument* aTEDocument;
  59.     aTEDocument = new TTEDocument;
  60.     FailNIL(aTEDocument);
  61.     aTEDocument->ITEDocument();
  62.     return aTEDocument;
  63. }
  64.  
  65. pascal void TTEApplication::PoseModalDialog()
  66. {
  67.     DialogPtr dPtr;
  68.     short    dItem;
  69.  
  70.     dPtr = GetNewDialog(kHelpID, nil, (WindowPtr) -1);
  71.     ModalDialog(nil, &dItem);
  72.     DisposDialog(dPtr);
  73. }
  74.  
  75.  
  76. pascal struct TCommand *TTEApplication::DoMenuCommand(CmdNumber aCmdNumber)
  77. {
  78.     switch (aCmdNumber) {
  79.         case cHelp: 
  80.             PoseModalDialog(); 
  81.             return gNoChanges;
  82.             break;        
  83.         default: return inherited::DoMenuCommand(aCmdNumber);
  84.     }
  85. }
  86.  
  87.  
  88. #ifdef qDebug
  89. pascal void TTEApplication::IdentifySoftware()
  90. {
  91.     ProgramReport("\pTEDemo ©J.Langowski/MacTutor June 1990",false);
  92.     inherited::IdentifySoftware();
  93. }
  94. #endif
  95.  
  96. // ---- Document ----
  97.  
  98. pascal void TTEDocument::ITEDocument()
  99. {
  100.     TList    *aList;
  101.     
  102.     IDocument(kFileType, kSignature, kUsesDataFork,
  103.                                 !kUsesRsrcFork, !kDataOpen, !kRsrcOpen);
  104.     fSavePrintInfo = true;    // save print info in data fork
  105.     fTextHdl = NewPermHandle(0);    // allocate block on heap for characters
  106.     FailNIL(fTextHdl);
  107.     fTextColorCmd = cBlack;
  108.     aList = NewList();    // make empty list of Boxs
  109.     fShapeList = aList;
  110. }
  111.  
  112. pascal void TTEDocument::AddShape(TBox *aBox)
  113. {
  114.     fShapeList->InsertFirst(aBox);
  115. }
  116.  
  117.  
  118. pascal void TTEDocument::DeleteShape()
  119. {
  120.     fShapeList->Delete(fShapeList->First());
  121. }
  122.  
  123.  
  124. pascal void TTEDocument::DoMakeViews(Boolean forPrinting)
  125. {
  126.     const Boolean kSquareDots = true;
  127.     const Boolean kFixedSize = true;
  128.     
  129.     TPaletteView        *aPaletteView;
  130.     TTextView            *aTextView;
  131.     TStdPrintHandler    *aStdHandler;
  132.  
  133.     aPaletteView = new TPaletteView;
  134.     FailNIL(aPaletteView);
  135.     aPaletteView->IPaletteView(this);
  136.     fPaletteView = aPaletteView;    // save reference to palette in document
  137.     
  138.     aTextView = new TTextView;
  139.     FailNIL(aTextView);
  140.     aTextView->ITextView(this);
  141.     fTextView = aTextView;    // save reference to text view in document
  142.     fTextView->StuffText(fTextHdl);        // so view uses the same characters
  143.     
  144.     aStdHandler = new TStdPrintHandler;    // make text view printable
  145.     FailNIL(aStdHandler);
  146.     aStdHandler->IStdPrintHandler(this,                 // document
  147.                                     aTextView,             // view to be printed
  148.                                     !kSquareDots,    // BOOLEAN for ImageWriter
  149.                                     kFixedSize,         // BOOLEAN horizontal page size
  150.                                     !kFixedSize);    // BOOLEAN vertical page size
  151. }
  152.  
  153. pascal void TTEDocument::DoMakeWindows()
  154. {
  155.     TWindow        *aWindow;
  156.  
  157.     aWindow = NewPaletteWindow(kWindID, kWantHScrollBar, kWantVScrollBar, 
  158.                     this, fTextView, fPaletteView, kPaletteHeight, kTopPalette);                    
  159.     aWindow->AdaptToScreen();    // adjust for various size monitors
  160.     aWindow->SimpleStagger(kStdStaggerAmount, kStdStaggerAmount, &gStdStaggerCount);
  161. }
  162.  
  163.  
  164. pascal void CalcDiskSpace(TBox *aBox, CalcDiskSpaceStruct *aCalcDiskSpaceStruct)
  165. {
  166.     aBox->NeedDiskSpace(&(aCalcDiskSpaceStruct->myDataForkBytes));
  167. }
  168.  
  169. pascal void TTEDocument::DoNeedDiskSpace(long *dataForkBytes, long *rsrcForkBytes)
  170. {
  171.     long textBytes,numberOfShapesBytes,cmdNumberBytes;
  172.     CalcDiskSpaceStruct aCalcDiskSpaceStruct;
  173.     
  174. /*    file format:
  175.         TextCmd Number (integer)
  176.         # shapes (integer)
  177.         data for shape #1
  178.         data for shape #2
  179.         etc.
  180.         text
  181. */
  182.     inherited::DoNeedDiskSpace(dataForkBytes, rsrcForkBytes); // to save print info
  183.     aCalcDiskSpaceStruct.myDataForkBytes = *dataForkBytes;
  184.     cmdNumberBytes = sizeof(int);
  185.     numberOfShapesBytes = sizeof(fTextColorCmd);
  186.     ForEachShapeDo((DoToObject)CalcDiskSpace,&aCalcDiskSpaceStruct);
  187.     textBytes = GetHandleSize(fTextHdl);
  188.     *dataForkBytes = cmdNumberBytes + numberOfShapesBytes + 
  189.                 aCalcDiskSpaceStruct.myDataForkBytes + textBytes;
  190. }
  191.  
  192.  
  193. pascal void TTEDocument::DoRead(short aRefNum,
  194.                         Boolean rsrcExists, Boolean forPrinting) 
  195. {
  196.     long    textColorCmdNumberBytes;
  197.     int        textColorCmdNumber;    
  198.     
  199.     long    numberOfShapesBytes;
  200.     int        numberOfShapes;
  201.     
  202.     TBox    *aBox;
  203.     long    eof, fPos, textBytes;
  204.     
  205. /*    file format:
  206.         TextCmd Number (integer)
  207.         # shapes (integer)
  208.         data for shape #1
  209.         data for shape #2
  210.         etc.
  211.         text
  212. */
  213.     inherited::DoRead(aRefNum, rsrcExists, forPrinting);    // print info to disk
  214.     
  215.     textColorCmdNumberBytes = sizeof(textColorCmdNumber);    // read text color
  216.     FailOSErr(FSRead(aRefNum, &textColorCmdNumberBytes, (Ptr) &textColorCmdNumber));
  217.     fTextColorCmd = textColorCmdNumber;
  218.  
  219.     numberOfShapesBytes = sizeof(numberOfShapes);    // read # items
  220.     FailOSErr(FSRead(aRefNum, &numberOfShapesBytes, (Ptr) &numberOfShapes));
  221.     for (int i = 1; i <= numberOfShapes; i++)
  222.     {
  223.         aBox = new TBox; // make new Box object
  224.         FailNIL(aBox);
  225.         aBox->Read(aRefNum);    // read and initialize object
  226.         AddShape(aBox);            // add to list in doc
  227.     };
  228.  
  229.     FailOSErr(GetFPos(aRefNum, &fPos));    // get size of text in file
  230.     FailOSErr(GetEOF(aRefNum, &eof));    // get size of data in file
  231.     textBytes = eof - fPos;
  232.     SetHandleSize(fTextHdl, textBytes);
  233.     HLock (fTextHdl);
  234.     FailOSErr(FSRead(aRefNum, &textBytes, *fTextHdl));    // read the text
  235.     HUnlock (fTextHdl);
  236. }
  237.  
  238.  
  239. pascal void DoToRect(TBox *aBox, DoToRectStruct    *aDoToRectStruct)
  240. {
  241.     aBox->Write(aDoToRectStruct->myRefNum);
  242. }
  243.     
  244. pascal void TTEDocument::DoWrite(short aRefNum, Boolean makingCopy)
  245. {
  246.     long    textColorCmdNumberBytes;
  247.     int        textColorCmdNumber;    
  248.     
  249.     long    numberOfShapesBytes;
  250.     int        numberOfShapes;
  251.     
  252.     long    textBytes;
  253.     DoToRectStruct    aDoToRectStruct;
  254.     
  255. /*    file format:
  256.         TextCmd Number (integer)
  257.         # shapes (integer)
  258.         data for shape #1
  259.         data for shape #2
  260.         etc.
  261.         text
  262. */
  263.     
  264.     inherited::DoWrite(aRefNum, makingCopy);    // print info to disk
  265.         
  266.     textColorCmdNumber = fTextColorCmd;    // write text color to disk
  267.     textColorCmdNumberBytes = sizeof(textColorCmdNumber);
  268.     FailOSErr(FSWrite(aRefNum, &textColorCmdNumberBytes, (Ptr) &textColorCmdNumber));
  269.     
  270.     numberOfShapes = fShapeList->fSize;    // write # items to disk
  271.     numberOfShapesBytes = sizeof(numberOfShapes);
  272.     FailOSErr(FSWrite(aRefNum, &numberOfShapesBytes, (Ptr) &numberOfShapes));
  273.     
  274.     aDoToRectStruct.myRefNum = aRefNum;
  275.     ForEachShapeDo((DoToObject)DoToRect,&aDoToRectStruct);    // write each rect to disk
  276.     
  277.     textBytes = GetHandleSize(fTextHdl);
  278.     HLock (fTextHdl);
  279.     FailOSErr(FSWrite(aRefNum, &textBytes, *fTextHdl));    // write the text
  280.     HUnlock (fTextHdl);
  281. }
  282.  
  283. pascal void TTEDocument::ForEachShapeDo(pascal void (*DoToItem)(TObject *item,
  284.                             void *DoToItem_Staticlink),void *DoToItem_Staticlink)
  285. {    fShapeList->Each(DoToItem,DoToItem_Staticlink);    }
  286.  
  287.  
  288. pascal void DisposeRect(TBox *aBox, void *link)
  289. {    aBox->Free();    }
  290.  
  291.  
  292. pascal void TTEDocument::Free()
  293. {
  294.     void *link;
  295.     
  296.     if (fTextHdl != nil) DisposHandle(fTextHdl);    
  297.                                     // dispose of ASCII characters
  298.     ForEachShapeDo((DoToObject)DisposeRect,link);    
  299.                                     // dispose of Box objects
  300.     fShapeList->Free();                // dispose of List object
  301.     inherited::Free();                // dispose of document object
  302. }
  303.  
  304.  
  305. #ifdef qDebug
  306. pascal void TTEDocument::Fields(pascal void (*DoToField) (StringPtr fieldName,
  307.                         Ptr fieldAddr, short fieldType, void *link), void *link)
  308. {    
  309.     DoToField("\pTTEDocument", nil, bClass, link);
  310.     DoToField("\pfPaletteView", (Ptr) &fPaletteView, bObject, link);
  311.     DoToField("\pfTextView", (Ptr) &fTextView, bObject, link);
  312.     DoToField("\pfTextHdl", (Ptr) &fTextHdl, bHandle, link);
  313.     DoToField("\pfTextColorCmd", (Ptr) &fTextColorCmd, bInteger, link);
  314.     DoToField("\pfShapeList", (Ptr) &fShapeList, bObject, link);
  315.     inherited::Fields(DoToField, link);
  316. }
  317. #endif
  318.  
  319. pascal void TTEDocument::SetTextColorCmd(int theColorCmd)
  320. {    fTextColorCmd = theColorCmd;    }
  321.  
  322.  
  323. pascal int  TTEDocument::TextColorCmd()
  324. {    return fTextColorCmd;        }
  325.  
  326.  
  327. //
  328. // ---- TPaletteView ----
  329. //
  330.  
  331. pascal void TPaletteView::IPaletteView(TTEDocument *itsTEDocument)
  332. {
  333.     VPoint    itsSize;
  334.  
  335.     SetVPt(&itsSize, kPaletteWidth, kPaletteHeight);
  336.     IView(itsTEDocument, nil, &gZeroVPt, &itsSize, sizeFixed, sizeFixed);
  337.     fIconSelected = kText-kBox;
  338. }
  339.  
  340.  
  341. pascal void TPaletteView::Draw(Rect *area)
  342. {
  343.     Rect    aFrame;
  344.     Point    aPenSize;
  345.     Handle    aHandle;
  346.     
  347.     ForeColor(kPaletteColor);
  348.     
  349.     aHandle = GetIcon(kBoxIconID);
  350.     FailNILResource(aHandle);
  351.     PlotIcon(&gIconRect[kBox-kBox], aHandle);
  352.     
  353.     aHandle = GetIcon(kTextIconID);
  354.     FailNILResource(aHandle);
  355.     PlotIcon(&gIconRect[kText-kBox], aHandle);
  356.     
  357.     ForeColor(blackColor);
  358.     GetQDExtent(&aFrame);
  359.     SetPt(&aPenSize, 1, 1);
  360.     Adorn(&aFrame, aPenSize, adnLineBottom);
  361. }
  362.  
  363. pascal void TPaletteView::DoHighlightSelection(HLState fromHL, HLState toHL)
  364. {
  365.     Rect    aRect;
  366.  
  367.     aRect = gIconRect[fIconSelected];
  368.     InsetRect(&aRect, 1, 1);
  369.     SetHLPenState(fromHL, toHL);
  370.     PaintRect(&aRect);
  371. }
  372.  
  373. pascal struct TCommand *TPaletteView::DoMouseCommand(Point *theMouse,
  374.                     EventInfo *info, Point *hysteresis)
  375. {
  376.     int    index;
  377.     
  378.     index = int(theMouse->h / kIconWidth);
  379.     
  380.     if ((index < 2) && (index != fIconSelected)) 
  381.     {
  382.         if (Focus()) { DoHighlightSelection(hlOn, hlOff);    };
  383.         fIconSelected = index;
  384.         if (Focus()) { DoHighlightSelection(hlOff, hlOn);    };
  385.     };
  386.     
  387.     return gNoChanges;
  388. }
  389.  
  390.  
  391. #ifdef qDebug
  392. pascal void TPaletteView::Fields(pascal void (*DoToField) (StringPtr fieldName,
  393.                         Ptr fieldAddr, short fieldType, void *link), void *link)
  394. {    
  395.     DoToField("\pTPaletteView", nil, bClass, link);
  396.     DoToField("\pfIconSelected", (Ptr) &fIconSelected, bInteger, link);
  397.     inherited::Fields(DoToField, link);
  398. }
  399. #endif
  400.  
  401.  
  402. pascal void TTextView::ITextView(TTEDocument *itsTEDocument)
  403. {    
  404.     VPoint    itsSize;
  405.     Rect     itsInset;
  406.     TextStyle    aStyle;
  407.     
  408.     SetVPt(&itsSize, 100, 100);
  409.     SetRect(&itsInset, 10, 8, 10, 0);
  410.     SetTextStyle(&aStyle, applFont, bold, 12, &gRGBBlack);
  411.     ITEView(itsTEDocument,    // document
  412.             nil,            // superview
  413.             &gZeroVPt,        // VPoint - itsLocation = topLeft of view
  414.             &itsSize,        // VPoint - width, height of view
  415.             sizePage,        // SizeDeterminer - width
  416.             sizeFillPages,    // SizeDeterminer - height
  417.             &itsInset,        // Rect - inset for TE's viewRect
  418.             &aStyle,        // TextStyle
  419.             teJustLeft,        // INTEGER - justification
  420.             !kWithStyle,    // BOOLEAN - styled TextEdit?
  421.             true);            // BOOLEAN - autoWrap?
  422.     fTEDocument = itsTEDocument;
  423.     fPaletteView = itsTEDocument->fPaletteView;
  424.     fUpdated = true;
  425. }
  426.  
  427. pascal Boolean TTextView::DoIdle(IdlePhase phase)
  428. {
  429.     if (!fUpdated)
  430.     {
  431.         if (Focus()) { DrawContents(); fUpdated = true; };
  432.         return inherited::DoIdle(phase);
  433.     }
  434. }
  435.  
  436.  
  437. pascal struct TCommand *TTextView::DoKeyCommand(short ch, 
  438.                                         short aKeyCode, EventInfo *info)
  439. {
  440.     int aQDColor;
  441.     
  442.     aQDColor = gColorArray[fTEDocument->TextColorCmd()-cBlack];
  443.     ForeColor(aQDColor);
  444.     fUpdated = false;
  445.     ForeColor(blackColor);
  446.  
  447.     return inherited::DoKeyCommand(ch, aKeyCode, info);
  448. }
  449.  
  450. pascal void TTextView::DoSetupMenus()
  451.     int colorIndex, aColorCmd;
  452.     
  453.     inherited::DoSetupMenus();    // always do this, so other objects get chance
  454.     aColorCmd = fTEDocument->TextColorCmd();
  455.     for (colorIndex = cBlack; colorIndex <= cWhite; colorIndex++)
  456.         EnableCheck(colorIndex, TRUE, (colorIndex == aColorCmd));
  457. }
  458.  
  459.  
  460. pascal struct TCommand *TTextView::DoMenuCommand(CmdNumber aCmdNumber)
  461. {
  462.     TColorCmd *aColorCmd;
  463.     
  464.     switch (aCmdNumber)
  465.     {
  466.         case cBlack:
  467.         case cBlue: 
  468.         case cGreen: 
  469.         case cRed: 
  470.         case cWhite: 
  471.             aColorCmd = new TColorCmd;
  472.             FailNIL(aColorCmd);
  473.             aColorCmd->IColorCmd(aCmdNumber, fTEDocument, this);
  474.             return aColorCmd;    // return that command object for MacApp
  475.         default:    // always do this, so other objects get a chance
  476.             return inherited::DoMenuCommand(aCmdNumber);
  477.  
  478.     }
  479. }
  480.  
  481. pascal struct TCommand *TTextView::DoMouseCommand(Point *theMouse, 
  482.                                     EventInfo *info, Point *hysteresis)
  483.     TSketcher *aSketcher;
  484.  
  485.     if (fPaletteView->fIconSelected == kBox-kBox) 
  486.     {
  487.         aSketcher = new TSketcher;
  488.         FailNIL(aSketcher);
  489.         aSketcher->ISketcher(fTEDocument, this);
  490.         return aSketcher;
  491.     }
  492.     else
  493.         return inherited::DoMouseCommand(theMouse,info,hysteresis);
  494. }
  495.  
  496.  
  497. pascal Boolean TTextView::DoSetCursor(Point localPoint, RgnHandle cursorRgn)
  498. {
  499.     Rect qdExtent;
  500.     
  501.     GetQDExtent(&qdExtent);
  502.     RectRgn(cursorRgn, &qdExtent);
  503.     if (fPaletteView->fIconSelected == kText-kBox)
  504.         return inherited::DoSetCursor(localPoint, cursorRgn); // use IBeam
  505.     else                // for box
  506.         SetCursor(*GetCursor(crossCursor));    // "+"
  507.     return true;
  508. }
  509.  
  510.  
  511.     
  512. pascal void DrawYourself(TBox *aBox, void *link)
  513. {
  514.     aBox->DrawShape();
  515. }
  516.  
  517. pascal void TTextView::Draw(Rect *area)
  518. {
  519.     int aQDColor;
  520.     void *link;
  521.     
  522.     aQDColor = gColorArray[fTEDocument->TextColorCmd()-cBlack];
  523.     ForeColor(aQDColor);    // set text color
  524.     inherited::Draw(area);    // let TTEView draw the text
  525.     PenNormal();
  526.     ForeColor(kBoxColor);    // set box color
  527.     fTEDocument->ForEachShapeDo((DoToObject)DrawYourself,link);
  528.     ForeColor(blackColor);
  529. }
  530.  
  531.  
  532. #ifdef qDebug
  533. pascal void TTextView::Fields(pascal void (*DoToField) (StringPtr fieldName,
  534.                         Ptr fieldAddr, short fieldType, void *link), void *link)
  535. {
  536.     DoToField("\pTTextView", nil, bClass, link);
  537.     DoToField("\pfTEDocument", (Ptr) &fTEDocument, bObject, link);
  538.     DoToField("\pfPaletteView", (Ptr) &fPaletteView, bObject, link);
  539.     DoToField("\pfUpdated", (Ptr) &fUpdated, bBoolean, link);
  540.     inherited::Fields(DoToField, link);
  541. }
  542. #endif
  543.  
  544. pascal void TColorCmd::IColorCmd(int aCmdNumber, 
  545.                     TTEDocument *itsDocument, TTextView *itsView)
  546. {
  547.     TScroller *aScroller;
  548.     int oldColorCmd;
  549.     
  550.     fTextView = itsView;
  551.     fTEDocument = itsDocument;
  552.     aScroller = itsView->GetScroller(true);
  553.     ICommand(aCmdNumber, itsDocument, itsView, aScroller);
  554.     oldColorCmd = itsDocument->TextColorCmd(); // menu command number
  555.     fOldColorCmd = oldColorCmd;    // menu command number
  556.     fNewColorCmd = aCmdNumber;    // menu command number
  557. }
  558.  
  559. pascal void TColorCmd::DoIt()
  560. {
  561.     fTEDocument->SetTextColorCmd(fNewColorCmd);
  562.     fTextView->ForceRedraw();
  563. }
  564.  
  565.  
  566. pascal void TColorCmd::RedoIt()
  567. {
  568.     UndoIt();
  569. }
  570.  
  571. pascal void TColorCmd::UndoIt()
  572. {
  573.     fNewColorCmd = fOldColorCmd;
  574.     fOldColorCmd = fTEDocument->TextColorCmd();
  575.     DoIt();    
  576. }
  577.  
  578.  
  579. #ifdef qDebug
  580. pascal void TColorCmd::Fields(pascal void (*DoToField) (StringPtr fieldName,
  581.                         Ptr fieldAddr, short fieldType, void *link), void *link)
  582. {
  583.     DoToField("\pTColorCmd", nil, bClass, link);
  584.     DoToField("\pfTEDocument", (Ptr) &fTEDocument, bObject, link);
  585.     DoToField("\pfTextView", (Ptr) &fTextView, bObject, link);
  586.     DoToField("\pfOldColorCmd", (Ptr) &fOldColorCmd, bInteger, link);
  587.     DoToField("\pfNewColorCmd", (Ptr) &fNewColorCmd, bInteger, link);
  588.     inherited::Fields(DoToField, link);
  589. }
  590. #endif
  591.  
  592.  
  593. pascal void TSketcher::ISketcher(TTEDocument *itsDocument, TTextView *itsView)
  594. {
  595.     TScroller *aScroller;
  596.  
  597.     aScroller = itsView->GetScroller(true);
  598.     ICommand(cDrawBox, itsDocument, itsView, aScroller);
  599.     fTEDocument = itsDocument;
  600.     fTextView = itsView;
  601. }
  602.  
  603.  
  604. pascal struct TCommand *TSketcher::TrackMouse(TrackPhase aTrackPhase,
  605.                             VPoint *anchorPoint, VPoint *previousPoint, 
  606.                             VPoint *nextPoint, Boolean mouseDidMove)
  607. {
  608.     Rect    newRect;
  609.     TBox    *aBox;
  610.     
  611.     if (aTrackPhase == trackRelease)
  612.     {
  613.         Pt2Rect(fTextView->ViewToQDPt(anchorPoint), 
  614.                     fTextView->ViewToQDPt(nextPoint), &newRect);
  615.         aBox = new TBox;
  616.         FailNIL(aBox);
  617.         aBox->IBox(&newRect);
  618.         fBox = aBox;
  619.         fBoxLocation = newRect;
  620.     }
  621.     return this;
  622. }
  623.  
  624. pascal void TSketcher::DoIt()
  625. {
  626.     fTEDocument->AddShape(fBox);
  627.     fTextView->InvalidRect(&fBoxLocation);
  628. }
  629.  
  630. pascal void TSketcher::RedoIt()  {  DoIt();  }
  631.  
  632. pascal void TSketcher::UndoIt()
  633. {    
  634.     fTEDocument->DeleteShape();
  635.     fTextView->InvalidRect(&fBoxLocation);
  636. }
  637.  
  638.  
  639. #ifdef qDebug
  640. pascal void TSketcher::Fields(pascal void (*DoToField) (StringPtr fieldName,
  641.                         Ptr fieldAddr, short fieldType, void *link), void *link)
  642. {
  643.     DoToField("\pTSketcher", nil, bClass, link);
  644.     DoToField("\pfTEDocument", (Ptr) &fTEDocument, bObject, link);
  645.     DoToField("\pfTextView", (Ptr) &fTextView, bObject, link);
  646.     DoToField("\pfBox", (Ptr) &fBox, bObject, link);
  647.     DoToField("\pfBoxLocation", (Ptr) &fBoxLocation, bRect, link);
  648.     inherited::Fields(DoToField, link);
  649. }
  650. #endif
  651.  
  652.  
  653. pascal void TBox::IBox(Rect *itsLocation)
  654.     {  fLocation = *itsLocation;  }
  655.  
  656. pascal void TBox::DrawShape()
  657. {
  658.     PenSize(4,4);
  659.     FrameRoundRect(&fLocation, 20,20);
  660. }
  661.  
  662. pascal void TBox::NeedDiskSpace(long *data)
  663. {
  664.     data = data + sizeof(fLocation);
  665. }
  666.  
  667. pascal void TBox::Read(short aRefNum)
  668. {
  669.     long bytes;
  670.     
  671.     bytes = sizeof(fLocation);    // bytes to store location
  672.     FailOSErr(FSRead(aRefNum, &bytes, (Ptr) &fLocation));
  673. }
  674.  
  675.  
  676. pascal void TBox::Write(short aRefNum)
  677. {
  678.     long bytes;
  679.     
  680.     bytes = sizeof(fLocation);    // bytes to store location
  681.     FailOSErr(FSWrite(aRefNum, &bytes, (Ptr) &fLocation));
  682. }
  683.  
  684. #ifdef qDebug
  685. pascal void TBox::Fields(pascal void (*DoToField) (StringPtr fieldName,
  686.                         Ptr fieldAddr, short fieldType, void *link), void *link)
  687. {
  688.     DoToField("\pTBox", nil, bClass, link);
  689.     DoToField("\pfLocation", (Ptr) &fLocation, bRect, link);
  690.     inherited::Fields(DoToField, link);
  691. }
  692. #endif
  693.  
  694.  
  695. TTEApplication *gTEApplication;
  696.  
  697. int main()
  698. {
  699.     InitToolBox();
  700.     if (ValidateConfiguration(&gConfiguration))
  701.     {
  702.         InitUMacApp(8);
  703.         InitUPrinting();
  704.         InitUTEView();
  705.         gTEApplication = new TTEApplication;
  706.         FailNIL(gTEApplication);
  707.         gTEApplication->ITEApplication(kFileType);
  708.         gTEApplication->Run();
  709.     }
  710.     else StdAlert(phUnsupportedConfiguration);
  711.     return 0;
  712. }
  713.